var pi = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;
var x_pi = (pi * 3000.0) / 180.0;
var R = 6378137;

function transform(lng, lat) {
  var dLat = transformLat(lng - 105.0, lat - 35.0);
  var dLng = transformLng(lng - 105.0, lat - 35.0);
  var radLat = (lat / 180.0) * pi;
  var magic = Math.sin(radLat);
  magic = 1 - ee * magic * magic;
  var sqrtMagic = Math.sqrt(magic);
  dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi);
  dLng = (dLng * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * pi);
  var mgLat = lat + dLat;
  var mgLng = lng + dLng;
  var newCoord = {
    lng: mgLng,
    lat: mgLat,
  };
  return newCoord;
}

function transformLat(x, y) {
  var ret =
    -100.0 +
    2.0 * x +
    3.0 * y +
    0.2 * y * y +
    0.1 * x * y +
    0.2 * Math.sqrt(Math.abs(x));
  ret +=
    ((20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0) /
    3.0;
  ret +=
    ((20.0 * Math.sin(y * pi) + 40.0 * Math.sin((y / 3.0) * pi)) * 2.0) / 3.0;
  ret +=
    ((160.0 * Math.sin((y / 12.0) * pi) + 320 * Math.sin((y * pi) / 30.0)) *
      2.0) /
    3.0;
  return ret;
}

function transformLng(x, y) {
  var ret =
    300.0 +
    x +
    2.0 * y +
    0.1 * x * x +
    0.1 * x * y +
    0.1 * Math.sqrt(Math.abs(x));
  ret +=
    ((20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0) /
    3.0;
  ret +=
    ((20.0 * Math.sin(x * pi) + 40.0 * Math.sin((x / 3.0) * pi)) * 2.0) / 3.0;
  ret +=
    ((150.0 * Math.sin((x / 12.0) * pi) + 300.0 * Math.sin((x / 30.0) * pi)) *
      2.0) /
    3.0;
  return ret;
}
/** 火星转84 */
this.gcj02_To_gps84 = function (lng, lat) {
  var coord = transform(lng, lat);
  var lontitude = lng * 2 - coord.lng;
  var latitude = lat * 2 - coord.lat;
  var newCoord = {
    lng: lontitude,
    lat: latitude,
  };
  return newCoord;
};

// 获取当前位置
function getPosition() {
  $.ajax({
    type: "get", // 接口规定，只能用get
    async: true, // 异步
    url: "https://apis.map.qq.com/ws/location/v1/ip", // 接口地址
    data: { key: "PTMBZ-GCQLW-SC2RG-R2FNI-HWPNQ-4PBQM", output: "jsonp" }, // 参数格式必须用到output传参为jsonp，否则会报跨域问题
    dataType: "jsonp", // 跨域，必须用到jsonp
    success: function (result) {
      console.log("result------", result);
      if (result.result && result.result.location) {
        let latlng = [result.result.location.lng, result.result.location.lat];
        let obj = gcj02_To_gps84(latlng[0], latlng[1]);
        map.setView([obj.lat, obj.lng], 16);
      }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      console.log(JSON.stringify(XMLHttpRequest));
      document.write(JSON.stringify(XMLHttpRequest));
    },
  });
}

let PositionBtn = document.getElementById("PositionBtn");
PositionBtn.onclick = function (params) {
  getPosition();
};

let btn = document.getElementById("btn");
btn.onclick = function (e) {
  gzGeoJSON.remove();
  initGeoJSON({
    url: "./geojson/py.json",
    centerLatlng: [22.97356659115516, 113.40156555175783],
    zoom: 12,
  });

  $("#mySelect").empty();
  tempRecordAddress = "";
  document.getElementById("mySelect").style.display = "none";
  document.getElementById("monitoring").style.display = "none";
  document.getElementById("monitoringCount").style.display = "none";
  document.getElementById("rechargeAnswer").style.display = "block";

  document.getElementById("second_level").style.display = "none";
  document.getElementById("first_level").style.display = "block";

  // $("#cenTitleText").html("番禺区充电站分布图");
  // 移除所有marker
  if (markerList.length > 0) {
    markerList.map((v) => {
      v && v.remove();
    });
  }
  curMarkerSn = "";
  getAlarmInfo();
};
